{extends file="./main.tpl"}
{block name=head}
<script type="text/javascript">
$(document).ready(function(){
 });
function chkWeight(theObj)
{
	var row = $(theObj).parents('.trRow');
  row.find(".calcQnty").removeAttr('checked');
  row.find(".calcWght").prop('checked', true);
	calculateAmount(theObj);
}

function chkQuantity(theObj)
{
	var row = $(theObj).parents('.trRow');
  row.find(".calcWght").removeAttr('checked');
  row.find(".calcQnty").prop('checked', true);
  calculateAmount(theObj);
}
function setRate(theFPid)
{
	var fpRow = $(theFPid).parents('.trRow');
	if($(theFPid).val() != '')
	{
		var dataString = "finalProcessId=" + $(theFPid).val() + "&partyId=" + $('#partyId').val();
			$.ajax({
			type: "GET",
			url: "setRate.php",
			data: dataString,
			success:function(data)
			{
				fpRow.find(".outRate").val(data);
				calculateAmount(theFPid);
		  }
		});
	}
}
function tickWght(theObject)
{
	var row = $(theObject).parents('.trRow');
	
  if(row.find(".checkBox").prop('checked'))
	{
		row.find(".calcWght").prop('checked', true);
	}
	else
  {
	  row.find(".finalProcess").val('');
	  row.find(".calcWght").removeAttr('checked');
	  row.find(".calcQnty").removeAttr('checked');
    row.find(".outRate").val('');	  
    row.find(".outAmount").val('');	  
	}
}
function calculateAmount(enteredValue)
{
	var row = $(enteredValue).parents('.trRow');
	
	var outQuantity = parseFloat(row.find(".outQuantity").val());
	var outWeight   = parseFloat(row.find(".outWeight").val());
	var rate        = parseFloat(row.find(".outRate").val());
	
	if(row.find(".calcWght").prop('checked'))
	{
	  row.find("#outAmount").val(rate * outWeight);
	  
	}
	else if(row.find(".calcQnty").prop('checked'))
	{
		row.find("#outAmount").val(rate * outQuantity);
	}
}
function setTotatOfEach()
{
	var setTotal = 0;
	
	$('.outAmount').each(function(i)
  {
    outAmount = $(this).val();
    if (!isNaN(outAmount)) setTotal += Number(outAmount);
  });
  $('#total').val(parseFloat(setTotal).toFixed(2));
  
  var netTotal = setTotal + parseFloat($("#serviceTax").val()) + parseFloat($("#roundOff").val());
  
  $('#netTotal').val(parseFloat(netTotal).toFixed(2));
}
function calcTaxRate(theTaxPercentage)
{
	var percentageTax = parseFloat($(theTaxPercentage).val());
	var taxOnAmount = parseFloat($('#total').val());
	var serviceTax = (percentageTax * taxOnAmount) / 100;
	$('#serviceTax').val(parseFloat(serviceTax).toFixed(2));
	setTotatOfEach();
}
</script>
{/block}
{block name=body}
<form name="createBill" action="{$smarty.server.PHP_SELF}" method="POST">
<table border="0" align="center" width="100%">
<tr>
  <td colspan="5"  align="right" class="table1"><font size="3"><b>Party</b></font></td>
  <td colspan="5" class="table1">                                                               
  <select name="partyId" id="partyId" onchange="this.form.submit();" autofocus="autofocus">
  <option value="" >Select</option>
  {html_options values=$partyArray.partyId output=$partyArray.partyName selected=$partyId}
  </select>
  </td>
</tr>
</table>
</form>

<form name="createBill" action="bill.php" method="POST">
<table border='0' cellpadding='1' cellspacing='2' align='center'>
<tr>
  <td colspan="14" class="table1" align="center"><font size="3">Bill Creation Process</font></td>
</tr>	
<tr>
	<td colspan="3" class="table2" align="center">Bill No :<input type="text" name="billId" value="{$billNo}" /></td>
	<td colspan="6" class="table2" align="center">Party : {$partyName}<input type="hidden" name="billPartyId" value="{$partyId}" /></td>
	<td colspan="5" class="table2" align="center">
		Date : {html_select_date prefix="billDate" start_year ="-0" end_year="+2" field_order="DMY" month_format="%m" day_value_format="%02d"}
	</td>
</tr>
<tr>
  <td  class="table1" align='center' valign="top"><b>&bull;</b></td>
  <td  class="table1" align='center' valign="top"><b>Out.<br>Challan</b></td>
  <td  class="table1" align='center' valign="top"><b>Date</b></td>
  <td  class="table1" align='center' valign="top"><b>In.<br>Challan</b></td>
  <td  class="table1" align='center' valign="top"><b>Material</b></td>
  <td  class="table1" align='center' valign="top"><b>Process<br>1</b></td>
  <td  class="table1" align='center' valign="top"><b>Process<br>2</b></td>
  <td  class="table1" align='center' valign="top"><b>Process<br>3</b></td>
  <td  class="table1" align='center' valign="top"><b>Final Process</b></td>
  <td  class="table1" align='center' valign="top"><b>Quantity</b></td>
  <td  class="table1" align='center' valign="top"><b>Weight</b></td>
  <td  class="table1" align='center' valign="top"><b>Calculation Of<br />Wght &nbsp; Qnty</b></td>
  <td  class="table1" align='center' valign="top"><b>Rate</b></td>
  <td  class="table1" align='center' valign="top"><b>Amount</b></td>
</tr>
{section name="sec" loop=$incomingRegister}
<tr class="trRow">
	<td class="table2" align="center">
		<input type="checkbox" name="checkedOutMasterId[{$incomingRegister[sec].rowCount}]" value="{$incomingRegister[sec].outgoingMasterId}" onclick="calculateAmount(this); tickWght(this);" class="checkBox" />
	</td>
  <td class="table2" align="center">{$incomingRegister[sec].outChallenNo}</td>
  <td class="table2" align="left" NOWRAP>{$incomingRegister[sec].IncomingDate}</td>
  <td class="table2" align="center">{$incomingRegister[sec].challan}</td>
  <td class="table2" align="center"NOWRAP>{$incomingRegister[sec].material}</td>
  <td class="table2" align="left"NOWRAP>{$incomingRegister[sec].processName1}</td>
  <td class="table2" align="left"NOWRAP>{$incomingRegister[sec].processName2}</td>
  <td class="table2" align="left"NOWRAP>{$incomingRegister[sec].processName3}</td>
  <td class="table2" align="left">
  	<select name="finalProcess[{$incomingRegister[sec].rowCount}]" class="finalProcess" onchange="setRate(this);">
  		<option value="">Select</option>
  		{html_options values=$fpArray.finalProcessId output=$fpArray.finalProcessName}
  	</select>
  </td>
  <td class="table2" align="right">{$incomingRegister[sec].outQuantity}<input type="hidden" name="outQuantity[{$incomingRegister[sec].rowCount}]" class="outQuantity" value="{$incomingRegister[sec].outQuantity}" /></td>
  <td class="table2" align="right">{$incomingRegister[sec].outWeight}<input type="hidden" name="outWeight[{$incomingRegister[sec].rowCount}]" class="outWeight" value="{$incomingRegister[sec].outWeight}" /></td>
  <td class="table2" align="center">
  	<input type="checkbox" name="calcWght[{$incomingRegister[sec].rowCount}]" class="calcWght" value="1" onclick="chkWeight(this); setTotatOfEach();" />
  	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  	<input type="checkbox" name="calcQnty[{$incomingRegister[sec].rowCount}]" class="calcQnty" value="1" onclick="chkQuantity(this); setTotatOfEach();" />
  </td>
  <td class="table2" align="right"><input type="text" name="outRate[{$incomingRegister[sec].rowCount}]" class="outRate" value="0" size="5" style="text-align:right;" onblur="calculateAmount(this); setTotatOfEach();" /></td>
  <td class="table2" align="right"><input type="text" name="outAmount[{$incomingRegister[sec].rowCount}]" class="outAmount" id="outAmount" size="10" style="text-align:right;" /></td>
</tr>
{sectionelse}
<tr>
	<td align="center" colspan="13">
   {$msg}
  </td>
</tr> 
{/section}
{if isset($incomingRegister[0].rowCount)}
<tr><td colspan="14"><hr></td></tr>
<tr>
	<td colspan="13" class="table2" align="right">Total :</td>
	<td class="table2"><input type="text" name="total" size="10" class="total" id="total" READONLY style="text-align:right;"  /></td>
</tr>
<tr>
	<td colspan="13" class="table2" align="right">Service Tax : <input type="text" size="1" name="taxInPrecentage" id="taxInPrecentage" class="taxInPrecentage" onblur="calcTaxRate(this);" style="text-align:right;" />%</td>
	<td class="table2"><input type="text" name="serviceTax" value="0" READONLY onblur="setTotatOfEach();" size="10" class="serviceTax" id="serviceTax" style="text-align:right;" /></td>
</tr>
<tr>
	<td colspan="13" class="table2" align="right">Round Off : </td>
	<td class="table2"><input type="text" name="roundOff" value="0" onblur="setTotatOfEach();" size="10" class="roundOff" id="roundOff" style="text-align:right;" /></td>
</tr>
<tr>
	<td colspan="13" class="table2" align="right">Net Total : </td>
	<td class="table2"><input type="text" name="netTotal" size="10" class="netTotal" id="netTotal" READONLY style="text-align:right;" /></td>
</tr>
<tr><td colspan="14" class="table2" align="center"><input type="submit" name="generateBill" value=" SUBMIT " /></td></tr>
{/if}
</table>
</form>
{/block}